package com.alm.token.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.base.BaseAdapter;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.data.bean.AddAppendOrderData;
import com.alm.token.ui.data.bean.BuyHMioAddData;
import com.alm.token.ui.data.bean.LH2OrderDetailsData;
import com.alm.token.ui.data.bean.UserWalletData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.QuantitativeOrder2View;
import com.alm.token.ui.viewpagefragment.BannerTwoFragment;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.alm.token.utils.StringUtils;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import baibeiwallet.activity.CreateWalletActivity1;
import baibeiwallet.activity.InprotWalletActivity1;
import baibeiwallet.utils.DialogUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QuantitativeSplashActivity extends BaseActivity implements QuantitativeOrder2View<AddAppendOrderData> {

    @BindView(R.id.tv_profitName)
    TextView tvProfitName;
    @BindView(R.id.tv_profit)
    TextView tvProfit;
    @BindView(R.id.tv_showStr)
    TextView tvShowStr;
    @BindView(R.id.tv_buytime)
    TextView tvBuytime;
    @BindView(R.id.tv_valuedate)
    TextView tvValuedate;
    @BindView(R.id.tv_settlementdate)
    TextView tvSettlementdate;
    @BindView(R.id.tv_guize)
    TextView tvGuize;
    @BindView(R.id.tv_showStr2)
    TextView tvShowStr2;
    @BindView(R.id.ly_order_xidatebody)
    LinearLayout lyOrderXidatebody;
    @BindView(R.id.tv_zonge_jiazhi)
    TextView tvZongeJiazhi;
    @BindView(R.id.rly_zonge_item)
    RelativeLayout rlyZongeItem;
    @BindView(R.id.tv_orderLevel)
    TextView tvOrderLevel;
    @BindView(R.id.ly_xinjibody)
    LinearLayout lyXinjibody;
    @BindView(R.id.tv_order_zhouqi)
    TextView tvOrderZhouqi;
    @BindView(R.id.tv_order_yuehua)
    TextView tvOrderYuehua;
    @BindView(R.id.tv_order_continuedDay)
    TextView tvOrderContinuedDay;
    @BindView(R.id.tv_order_expirationdate)
    TextView tvOrderExpirationDate;
    @BindView(R.id.tv_order_shouyi)
    TextView tvOrderShouyi;
    @BindView(R.id.rly_shouyi_item)
    RelativeLayout rlyShouyiItem;
    @BindView(R.id.tv_order_shenfen)
    TextView tvOrderShenfen;
    @BindView(R.id.tv_order_shouxubili)
    TextView tvOrderShouxubili;
    @BindView(R.id.tv_order_shouxu)
    TextView tvOrderShouxu;
    @BindView(R.id.rly_shouxu_item)
    RelativeLayout rlyShouxuItem;
    @BindView(R.id.ly_orderbody)
    LinearLayout lyOrderbody;
    @BindView(R.id.btn_quantitativeapply_click)
    TextView btnQuantitativeapplyClick;
    @BindView(R.id.btn_quantitativeapply_appendclick)
    TextView btnQuantitativeapplyAppendclick;
    @BindView(R.id.btn_quantitativeapply_closeclick)
    TextView btnQuantitativeapplyCloseclick;
    @BindView(R.id.recycler_view_zonge)
    RecyclerView recyclerViewZonge;
    @BindView(R.id.recycler_view_shouxu)
    RecyclerView recyclerViewShouxu;
    @BindView(R.id.tv_zonge_more)
    TextView tvZongeMore;
    @BindView(R.id.tv_shouyi_more)
    TextView tvShouyiMore;
    @BindView(R.id.tv_shouxu_more)
    TextView tvShouxuMore;

    private RefreshLoadMoreManager<LH2OrderDetailsData.DataBean.ZongeBean> managerZonge;
    private RefreshLoadMoreManager<LH2OrderDetailsData.DataBean.ShouxuBean> managerShouxu;
    private List<LH2OrderDetailsData.DataBean.ZongeBean> zonge = new ArrayList<>();
    private List<LH2OrderDetailsData.DataBean.ShouxuBean> shouxu = new ArrayList<>();
    private int ActivityForResultCode = 1;
    private LH2OrderDetailsData.DataBean OrderDetailsDataBean;

    public QuantitativeSplashActivity() {
    }

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }


    @Override
    protected int getContentViewId() {
        return R.layout.activity_quantitative_splash;
    }

    @Override
    protected void initVariables() {

    }


    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.fortune_hint_alpha)).setRightIcon(R.drawable.lh_order, this);
        lyOrderbody.setVisibility(View.GONE);
        lyOrderXidatebody.setVisibility(View.GONE);
        initManager();
    }

    private void initManager() {
        OrderAdapter adapterZonge = new OrderAdapter(this);
        managerZonge = new RefreshLoadMoreManager.Builder(this)
                .setAdapter(adapterZonge)
                .setRecyclerView(recyclerViewZonge)
                .build();
        OrderAdapter2 adapterShouxu = new OrderAdapter2(this);
        managerShouxu = new RefreshLoadMoreManager.Builder(this)
                .setAdapter(adapterShouxu)
                .setRecyclerView(recyclerViewShouxu)
                .build();

        recyclerViewZonge.setNestedScrollingEnabled(false);
        recyclerViewShouxu.setNestedScrollingEnabled(false);
    }

    @Override
    protected void loadData() {
        preenter.getLH2OrderDetailsData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rightItem:
                startActivity(LH2OrderHistoryActivity.class);
                break;
        }
    }

    @OnClick({R.id.btn_quantitativeapply_click, R.id.rly_scanroal_item, R.id.rly_zonge_item, R.id.rly_shouyi_item, R.id.rly_shouxu_item,
            R.id.btn_quantitativeapply_appendclick, R.id.btn_quantitativeapply_closeclick})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_quantitativeapply_appendclick:
                Intent intent = new Intent(QuantitativeSplashActivity.this, Quantitative20OrderOpenAddActivity.class);
                intent.putExtra("intentType", 1);
                intent.putExtra("totalmoney", Double.parseDouble(CommonUtil.saveToPoint2WithNo(jiazhi)));
                startActivityForResult(intent, ActivityForResultCode);
                break;
            case R.id.btn_quantitativeapply_click:
//                preenter.getLH2OrderNoOrderShowData();
                startActivityForResult(Quantitative20OrderOpenAddActivity.class, ActivityForResultCode);
                break;
            case R.id.btn_quantitativeapply_closeclick:
                showPopupMenu(btnQuantitativeapplyCloseclick);
//                preenter.getLH2OrderPartOrderShowData();
                //查看规则
                break;
            case R.id.rly_scanroal_item:
                //查看规则
                break;
            case R.id.rly_zonge_item:
                recyclerViewZonge.setVisibility(recyclerViewZonge.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
                //查看规则
                break;
            case R.id.rly_shouyi_item:
                //查看规则
                startActivity(QuantitativeShouyiListActivity.class);
                break;
            case R.id.rly_shouxu_item:
                //查看规则
                recyclerViewShouxu.setVisibility(recyclerViewShouxu.getVisibility() == View.GONE ? View.VISIBLE : View.GONE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == ActivityForResultCode) {
            if (resultCode == QuantitativeOrderOpenAddActivity.AddAppendSucc) {
//                showToast("添加成功，刷新列表");
                loadData();
                setResult(QuantitativeOrderOpenAddActivity.AddAppendSucc);
//                finish();
            }
            if (resultCode == QuantitativeOrderDetailActivity.AppendSucc) {
//                showToast("添加成功，刷新列表");
                loadData();
                setResult(QuantitativeOrderDetailActivity.AppendSucc);
//                finish();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    public void getData(AddAppendOrderData addAppendOrderData) {

        if (addAppendOrderData.getStatus() == 0) {
            loadData();
            setResult(QuantitativeOrderDetailActivity.AppendSucc);
        } else {
            showToast(addAppendOrderData.getMsg());
        }
    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(),PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {
        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }

    double jiazhi = 0.00;
    @Override
    public void getLH2OrderOrderDetails(LH2OrderDetailsData lh2OrderDetailsData) {

        if (lh2OrderDetailsData.getStatus() == 0) {
            OrderDetailsDataBean = lh2OrderDetailsData.getData();

            if (EmptyUtils.isNotEmpty(OrderDetailsDataBean)) {
                tvProfitName.setText(OrderDetailsDataBean.getProfitName());
                tvProfit.setText(OrderDetailsDataBean.getProfit());
                tvShowStr.setText(OrderDetailsDataBean.getShowStr());
                tvBuytime.setText(CommonUtil.getDateTimeFromMillisecond3(OrderDetailsDataBean.getAddDate()));
                tvValuedate.setText(CommonUtil.getDateTimeFromMillisecond3(OrderDetailsDataBean.getQixi()));
                tvSettlementdate.setText(CommonUtil.getDateTimeFromMillisecond3(OrderDetailsDataBean.getDaoqi()));
                tvGuize.setText(OrderDetailsDataBean.getGuize());
                tvShowStr2.setText(OrderDetailsDataBean.getShowStr2());

                if (EmptyUtils.isNotEmpty(OrderDetailsDataBean.getOrdersn())) {
                    lyOrderXidatebody.setVisibility(View.GONE);
                    lyOrderbody.setVisibility(View.VISIBLE);


                    if (EmptyUtils.isNotEmpty(OrderDetailsDataBean.getZonge())) {
                        zonge = OrderDetailsDataBean.getZonge();
                        jiazhi=0;
                        for (LH2OrderDetailsData.DataBean.ZongeBean ZongeBean : zonge) {
                            jiazhi += ZongeBean.getNum()*ZongeBean.getPrice();
                            ZongeBean.setLayoutId(OrderAdapter.LAYOUT_ID);
                        }
                        tvZongeJiazhi.setText("$"+CommonUtil.saveToPoint2WithNo(jiazhi) + "\t\t\t");
                        tvZongeMore.setVisibility(View.VISIBLE);
                        managerZonge.setData(zonge);
                    } else {
                        tvZongeJiazhi.setText("$"+"0.00");
                        tvZongeMore.setVisibility(View.GONE);
                    }

                    tvOrderLevel.setText("AGOL" + OrderDetailsDataBean.getXingji());
                    int ll1 = OrderDetailsDataBean.getXingji();
                    if (ll1 <= lyXinjibody.getChildCount()) {
                        for (int i = 0; i < lyXinjibody.getChildCount(); i++) {//4number  i=0-3
                            if (i < ll1) { //i=0 1 2 3  ll1=1 2 3 4
                                lyXinjibody.getChildAt(i).setVisibility(View.VISIBLE);
                            } else {
                                lyXinjibody.getChildAt(i).setVisibility(View.GONE);
                            }
                        }
                    }

                    tvOrderZhouqi.setText(OrderDetailsDataBean.getZhouqi() + getString(R.string.lh20_hint_day));
                    tvOrderYuehua.setText("≈" + OrderDetailsDataBean.getYuehua() + "%/" + getString(R.string.alphago_hint_month));
                    tvOrderContinuedDay.setText(OrderDetailsDataBean.getContinuedDay() + getString(R.string.lh20_hint_day));

                    tvOrderExpirationDate.setText(CommonUtil.getDateTimeFromMillisecond3(OrderDetailsDataBean.getOverDate()));
                    tvOrderShouyi.setText(StringUtils.nullToStr0(OrderDetailsDataBean.getShouyi()));
                    tvShouyiMore.setVisibility(EmptyUtils.isEmpty(OrderDetailsDataBean.getShouyi()) ? View.GONE : View.VISIBLE);
                    tvOrderShenfen.setText(OrderDetailsDataBean.getShenfen());
                    tvOrderShouxubili.setText(OrderDetailsDataBean.getShouxubili() + "%");

                    if (EmptyUtils.isNotEmpty(OrderDetailsDataBean.getShouxu())) {
                        shouxu = OrderDetailsDataBean.getShouxu();
                        String shouxustr = "";
                        for (int i = 0; i < shouxu.size(); i++) {
                            shouxustr = shouxustr + shouxu.get(i).getShouxufei() + shouxu.get(i).getName() + (i != shouxu.size() - 1 ? "+" : "");
                            shouxu.get(i).setLayoutId(OrderAdapter2.LAYOUT_ID);
                        }
                        tvOrderShouxu.setText(shouxustr + "\t\t\t");
                        tvShouxuMore.setVisibility(View.VISIBLE);
                        managerShouxu.setData(shouxu);
                    } else {
                        tvOrderShouxu.setText("0.00");
                        tvShouxuMore.setVisibility(View.GONE);
                    }
                    btnQuantitativeapplyClick.setVisibility(View.GONE);
                    btnQuantitativeapplyAppendclick.setVisibility(View.VISIBLE);
                    btnQuantitativeapplyCloseclick.setVisibility(View.VISIBLE);
                } else {
                    lyOrderXidatebody.setVisibility(View.VISIBLE);
                    lyOrderbody.setVisibility(View.GONE);
                    btnQuantitativeapplyClick.setVisibility(View.VISIBLE);
                    btnQuantitativeapplyAppendclick.setVisibility(View.GONE);
                    btnQuantitativeapplyCloseclick.setVisibility(View.GONE);
                }
            }
        } else {
            showToast(lh2OrderDetailsData.getMsg());
        }
    }

    @Override
    public void getLH2OrderOrderDetails(BuyHMioAddData buyHMioAddData) {

    }


    public class OrderAdapter extends BaseAdapter<LH2OrderDetailsData.DataBean.ZongeBean> {

        public static final int LAYOUT_ID = R.layout.item_quantitative_order_detail_date_item;

        public OrderAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, LH2OrderDetailsData.DataBean.ZongeBean item, int itemType) {

            helper.setText(R.id.tv_shouyitime, item.getName());
            helper.setTextColor(R.id.tv_shouyitime, context.getResources().getColor(R.color.text_2223292));
            helper.setText(R.id.tv_shouymoney, CommonUtil.saveToPoint4WithNo(item.getNum()) + "*" + CommonUtil.saveToPoint4WithNo(item.getPrice()));
            helper.setTextColor(R.id.tv_shouymoney, context.getResources().getColor(R.color.bg_653fac2));
        }

    }

    public class OrderAdapter2 extends BaseAdapter<LH2OrderDetailsData.DataBean.ShouxuBean> {

        public static final int LAYOUT_ID = R.layout.item_quantitative_order_detail_date_item;

        public OrderAdapter2(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, LH2OrderDetailsData.DataBean.ShouxuBean item, int itemType) {
            helper.setText(R.id.tv_shouyitime, item.getName());
            helper.setTextColor(R.id.tv_shouyitime, context.getResources().getColor(R.color.text_2223292));
            helper.setText(R.id.tv_shouymoney, CommonUtil.saveToPoint4WithNo(item.getShouxufei()));
            helper.setTextColor(R.id.tv_shouymoney, context.getResources().getColor(R.color.bg_653fac2));
        }

    }

    public void showPopupMenu(View view) {
        //参数View 是设置当前菜单显示的相对于View组件位置，具体位置系统会处理
        PopupMenu popupMenu = new PopupMenu(this, view);
        //加载menu布局
        popupMenu.getMenuInflater().inflate(R.menu.menu_popmenu_closeselect, popupMenu.getMenu());

        //设置menu中的item点击事件
        popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

                switch (item.getItemId()) {
                    case R.id.all_close:
                        UserWalletData.DataBean.SysMsgBean sysMsgBean = new UserWalletData.DataBean.SysMsgBean();
                        sysMsgBean.setTitle(getString(R.string.alphago_hint_warn9));
                        sysMsgBean.setContent(getString(R.string.alphago_hint_warn10));
                        DialogUtils.showInfor1(QuantitativeSplashActivity.this, sysMsgBean, (pwd) -> {

                            DialogUtils.enterPayPsw(QuantitativeSplashActivity.this, (pwd1) -> {
                                preenter.getLH2OrderCloseOrderData(OrderDetailsDataBean.getOrdersn(), pwd1);
                            });

                        });
                        break;

                    case R.id.some_close:
                        Intent intent = new Intent(QuantitativeSplashActivity.this, Quantitative20OrderCloseActivity.class);
                        intent.putExtra("Ordersn", OrderDetailsDataBean.getOrdersn());
                        startActivityForResult(intent, ActivityForResultCode);
                        break;

                }
                return true;
            }
        });
        popupMenu.show();
    }
}
